<?php

/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
class LM_Table_Teammap extends LM_Table
{
    /** @var int Primary Key */
    public $teammap_id;
    /** @var int references LM_Table_Project */
    public $teammap_project;
    /** @var int references LM_Table_Team */
    public $teammap_team;
    /** @var int references mosUser */
    public $teammap_user;
    /** @var int references LM_Table_Player */
    public $teammap_trainer;
    /** @var int start points */
    public $teammap_points;
    /** @var string info */
    public $teammap_info;
    /** @var string image */
    public $teammap_image;
    /** @var string text */
    public $teammap_text;
    /** @var int showResults */
    public $showResults;
    /** @var int showTable */
    public $showTable;
    /** @var int showSquad */
    public $showSquad;
    /** @var int showHistory */
    public $showHistory;
    /** @var int showClub */
    public $showClub;
    /** @var int showNews */
    public $showNews;

    /** @var object LM_Table_Team */
    private $_team;

    public function __construct($db)
    {
        parent::__construct('#__leagueman_teammap', 'teammap_id', $db);
    }

    public function getID()
    {
        return $this->teammap_id;
    }

    public function getProjectID()
    {
        return $this->teammap_project;
    }

    public function getTeamID()
    {
        return $this->teammap_team;
    }

    public function getUserID()
    {
        return $this->teammap_user;
    }

    public function getImage()
    {
        return $this->teammap_image;
    }

    public function getPoints()
    {
        return $this->teammap_points;
    }

    public function getInfo()
    {
        return $this->teammap_info;
    }

    public function getText()
    {
        return $this->teammap_text;
    }

    public function getRows($limitstart = 0, $limit = 10)
    {
        $sql = "SELECT tt.*, t.*, u.username as admin
                  FROM #__leagueman_teammap tt
            INNER JOIN #__leagueman_team t on tt.teammap_team = t.team_id
             LEFT JOIN #__users u on tt.teammap_user = u.id"
             . (count($this->_where) > 0 ? " WHERE " . implode(" AND ", $this->_where) : " ");
        $this->_db->setQuery($sql);
        return $this->_db->loadObjectList();
    }

    public function getTotalRows()
    {
        $sql = "SELECT COUNT(tm.teammap_id)
                  FROM #__leagueman_teammap tm
            INNER JOIN #__leagueman_team t on tm.teammap_team = t.team_id
             LEFT JOIN #__users u on tm.teammap_user = u.id"
             . (count($this->_where) > 0 ? " WHERE " . implode(" AND ", $this->_where) : " ");
        $this->_db->setQuery($sql);
        return $this->_db->loadResult();
    }

    public function filterbyProject($project_id)
    {
        $this->addFilter("tm.teammap_project = " . $this->_db->Quote(intval($project_id)));
    }

    public function filterbyTeam($team_id)
    {
        $this->addFilter("tm.teammap_team = " . $this->_db->Quote(intval($team_id)));
    }

    public function loadTeam()
    {
        $this->_team = LM::getLMTable("Team");
        $this->_team->load($this->teammap_team);
    }

    public function getTeam()
    {
        if (!$this->_team instanceof LM_Table_Team) {
            $this->loadTeam();
        }

        return $this->_team;
    }

    public function getPlayers()
    {
        $sql = "SELECT * FROM #__leagueman_playermap
                 WHERE playermap_project = " . $this->getProjectID() . "
                   AND playermap_team = " . $this->getTeamID();
        $this->_db->setQuery($sql);
        return $this->_db->loadObjectList();
    }

    public function hasOption($option)
    {
        if (in_array($option, array('showResults', 'showTable', 'showSquad', 'showHistory', 'showClub', 'showNews'))) {
            return $this->$option;
        }

        return false;
    }

    public function hasOptions()
    {
        return $this->showResults == 1 || $this->showTable == 1 || $this->showSquad == 1 || $this->showHistory == 1 || $this->showClub == 1 || $this->showNews == 1;
    }

    public function toggleOption($option)
    {
        if (in_array($option, array('showResults', 'showTable', 'showSquad', 'showHistory', 'showClub', 'showNews'))) {
            $this->$option = 1 - $this->$option;
            $this->store();
        }
    }
}